package com.easy.sftp.pool;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;

import java.util.Properties;

public class Connection<E> {

    private static final Logger logger = LoggerFactory.getLogger(Connection.class);

    public static <E> E connect(String username,String ip, String password, Integer port, String type) throws JSchException {
        JSch jsch = new JSch();
        Session session = jsch.getSession(username, ip, port);
        session.setPassword(password);
        Properties sshConfig = new Properties();
        sshConfig.put("StrictHostKeyChecking", "no");
        session.setConfig(sshConfig);
        session.connect();
        logger.info("连接成功: {}:{}/{}", username, password, port);
        Channel channel = session.openChannel(type);
        Assert.notNull(channel, "open channel fail");
        channel.connect();
        return (E) channel;
    }


}
